import { Toast } from 'vant'

function login (ckd, phone, password) {
  // 将JSON字符串转换成为JSON对象输出
  const storage = window.localStorage
  // let jsonStr=storage.getItem("userList");
  const userList = JSON.parse(storage.getItem('userList'))
  console.log(userList)
  // 判断表单是否为空
  if (ckd === false) {
    Toast.fail('请勾选隐私政策')
    return false
  } else {
    if (phone === '' || password === '' || password === '') {
      Toast.fail('请输入内容')
      return false
    } else {
      const phoneReg = /^(0|86|17951)?(13[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57])[0-9]{8}$/
      const passReg = /^\d+$/

      if (!window.localStorage) {
        alert('浏览器不支持localstorage')
        return false
      } else if (!phoneReg.test(phone)) {
        Toast.fail('该手机号未注册，请先去注册或重新输入')
        return false
      } else if (!passReg.test(password)) {
        Toast.fail('密码格式错误，只能由数字组成')
        return false
      } else if (phoneReg.test(phone) && passReg.test(password)) {
        let str = 'false'
        for (const key in userList) {
          if (phone === userList[key].phone && password === userList[key].password) {
            location.href = 'http://10.7.164.124:20000/pay'
            Toast.success('登录成功')
            const jsonStr = JSON.stringify(phone)
            storage.setItem('phone', jsonStr)
            str = 'ture'
          }
        }
        if (str === 'false') {
        //   alert('电话号码或密码错误，请重试')
          Toast.fail('电话号码或密码错误，请重试')
          return false
        }
      }
    }
  }
}

export default login
